package com.myapp.struts.Social.ReadMessage;

import com.myapp.model.estados.Estados;
import com.myapp.model.mensajes.Mensajes;
import com.myapp.svc.Notifications.NotificationsSvc;
import com.myapp.svc.Social.SocialSvc;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ReadMessageAction extends org.apache.struts.action.Action {

    /*
     * forward name="success" path=""
     */
    private static final String SUCCESS = "success";

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        HttpSession sesion = request.getSession();
        Mensajes m = new Mensajes();
        m.setIdmensaje(Integer.parseInt(request.getParameter("idmensaje")));
        SocialSvc fsvc = new SocialSvc();
        Mensajes me = fsvc.readMessage(m);
        request.setAttribute("mensaje", me);       
        if (me.getEstados().getIdestado() == 10) {
            ArrayList al = (ArrayList) sesion.getAttribute("contador");
            String nc = String.valueOf((Integer.parseInt(al.get(1).toString()) - 1));
            al.set(1, nc);
            sesion.setAttribute("contador", al);
            NotificationsSvc nsvc = new NotificationsSvc();
            List n = (ArrayList) sesion.getAttribute("notificaciones");
            nsvc.changeStateMessageNotifications(n, me);
        }
        return mapping.findForward(SUCCESS);
    }
}
